package com.drei.wolke.server.oauth2.controller;

import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.drei.wolke.server.oauth2.token.store.RedisTemplateTokenStore;

/**
 * @author 作者 owen E-mail: 624191343@qq.com
 * @version 创建时间：2017年11月12日 上午22:57:51
 */
@RestController
public class UserController {
	private static final Logger logger = LoggerFactory.getLogger(UserController.class);

	@Resource
	private RedisTemplate<String, Object> redisTemplate;

	@GetMapping("/hello")
	public String hello() {
		return "hello";
	}

	@RequestMapping(value = { "/user" }, produces = "application/json") // 获取用户信息。/auth/user
	public Map<String, Object> user(OAuth2Authentication user) {
		Map<String, Object> userInfo = new HashMap<>();
		userInfo.put("user", user.getUserAuthentication().getPrincipal());
		logger.debug("认证详细信息:" + user.getUserAuthentication().getPrincipal().toString());
		userInfo.put("authorities", AuthorityUtils.authorityListToSet(user.getUserAuthentication().getAuthorities()));
		return userInfo;
	}

	@RequestMapping(value = { "/getUserFromToken" }, produces = "application/json") // 获取用户信息。/auth/user
	public Map<String, Object> getUserFromToken(String token) throws Exception {
		String tokenKey = "auth:" + token;

		Map<String, Object> userInfo = new HashMap<>();
		if (!redisTemplate.hasKey(tokenKey)) {
			return null;
		}

		OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) redisTemplate.opsForValue().get(tokenKey);

		userInfo.put("user_name", oAuth2Authentication.getUserAuthentication().getPrincipal().toString()); 
		userInfo.put("client_id", oAuth2Authentication.getOAuth2Request().getClientId());
		return userInfo;
	}

	@GetMapping("/del/{accessToken}/{refreshToken}")
	public String hello2(@PathVariable String accessToken, @PathVariable String refreshToken) {
		RedisTemplateTokenStore redisTemplateStore = new RedisTemplateTokenStore();
		redisTemplateStore.setRedisTemplate(redisTemplate);
		redisTemplateStore.removeAccessToken(accessToken);
		redisTemplateStore.removeRefreshToken(refreshToken);
		return "delR";
	}
}
